ManagedModule = function(obj) {
	this.id = new Date().getTime();
	this.url = null;
	this.title = null;
	this.type = null;
	if (obj != null) {
		this.id = obj.id;
		this.url = obj.url;
		this.title = obj.title;
		this.type = obj.type;
	}
}

function init_options() {
	document.getElementById("url_prefix").onchange = save_options;
	document.getElementById("post_url").onchange = save_options;
	document.getElementById("add_managed_module").onclick = add_managed_module;
	var lurl_prefix = dao.getUrlPrefix();
	var lpost_url = dao.loadPostUrl();
	if (lurl_prefix != null)
		document.getElementById("url_prefix").value = lurl_prefix;
	if (lpost_url != null)
		document.getElementById("post_url").value = lpost_url;
	build_managed_area();
}

function save_options() {
	var url_prefix = document.getElementById("url_prefix").value;
	var post_url = document.getElementById("post_url").value;
	if (url_prefix != '') 
		dao.saveUrlPrefix(url_prefix);
	if (post_url != '')
		dao.savePostUrl(post_url);
}

function add_managed_module() {
	var mm = new ManagedModule();
	mm.url = get_url_value("managed_module");
	if (mm.url == null) return;
	mm.type = get_sel_value("mm_type");
	var managed_modules = dao.loadManagedModules();
	managed_modules.push(mm);
	dao.saveManagedModules(managed_modules);
	build_managed_area();
}

function delete_managed_module() {
	var url = event.srcElement.url;
	var managed_modules = dao.loadManagedModules();
	for (var i in managed_modules) {
		var mm = managed_modules[i];
		if (mm.url == url) {
			managed_modules.splice(i, 1);
		}
	}
	dao.saveManagedModules(managed_modules);
	build_managed_area();
}

function build_managed_area(type, div) {
	var mm_div = document.getElementById("managed_modules_area");
	mm_div.innerHTML = "";
	var ary = dao.loadManagedModules();
	var unknown = new Array();
	for (var i in ary) {
		var mm = ary[i];
		var div = document.createElement("div");
		var del_btn = document.createElement("input");
		del_btn.type = 'button';
		del_btn.value = '-';
		del_btn.url = mm.url;
		del_btn.onclick = delete_managed_module;
		div.appendChild(del_btn);
		var div_name = document.createElement("span");
		div_name.className = 'managed_module_span';
		div_name.url = mm.url;
		if (mm.title == null) {
			unknown.push(mm.url);
			div_name.innerText = '标题获取中。。。' + "(" + mm.url + ")";
		} else
			div_name.innerHTML = build_mm_html(mm);
		div.appendChild(div_name);
		mm_div.appendChild(div);
	}
	get_titles(unknown, set_managed_module_title);
}

function set_managed_module_title(url, title) {
	var managed_modules = dao.loadManagedModules();
	for (var i in managed_modules) {
		var mm = managed_modules[i];
		if (managed_modules[i].url == url) {
			managed_modules[i].title = title;
			break;
		}
	}
	dao.saveManagedModules(managed_modules);
	var mm_div = document.getElementById("managed_modules_area");
	var spans = mm_div.getElementsByClassName("managed_modules_span");
	for (var i in spans) {
		var span = spans[i];
		if (span.url == url) {
			span.innerHTML = build_mm_html(mm);
			break;
		}
	}
}

function build_mm_html(mm) {
	return '<a href="' + append_prefix(mm.url) + '" target="_blank">' + mm.title + '</a>&nbsp;(' + get_mm_type_desc(mm.type) + ')';
}

function get_mm_type_desc(type) {
	switch (type) {
		case 'mm':
			return '管理模块';
		case 'ss':
			return '签名统计';
		default:
			return '未知类型';
	}
}
